const express = require('express');
const config = require('./config')
const app = express();
const Model = require('./model/index');
const bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();

// 解析body传参   解析json
app.use(express.urlencoded({extended:false}))
app.use(express.json());

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

//解析form-data
app.use(upload.array());
app.use(express.static('public'));

// 导入各个控制器

const AdminRouter = require('./routers/Admin')
const GoodsRouter = require('./routers/Goods')
const UserRouter = require('./routers/User')

// 通过一个全局中间件 把model全部挂载到 req上面
app.use((req,res,next)=>{
    req.Model = Model;
    next();
})

// 跨域 cors
const cors = require('cors')
app.use(cors()); // 解除cors跨域限制
const mongoose = require('./db')  // 把刚才配置的mongoose链接导入


app.use('/admin',AdminRouter);
app.use('/goods',GoodsRouter);
app.use('/user',UserRouter);


app.listen(config.appPort,()=>{
    console.log('srv is running at port 127.0.0.1:'+ config.appPort)
})